<template>

  <card-component
    :content-title="contentTitle"
    :page="page"
    @handleSizeChange="handleSizeChange"
    @handleCurrentChange="handleCurrentChange"
>


    <table-component slot="table"
                     :table-header="tableHeader"
                     :table-data="tableData"
                     :page="page"
    ></table-component>


  </card-component>

</template>

<script>

  import card from '@/base/card.vue'
  import table from '@/base/AutoWidthTable.vue'

  import request from '@/common/js/request'

  import util from '@/common/js/util'

  export default {
    components: {
      'card-component': card,
      'table-component': table,
    },
    data() {
      return {
        contentTitle: this.$getTextByLangKey(""),
        tableHeader: [],
        tableData: [],
        page: {
          currentPage: 1,
          total: 0,
          pageSize: 20,
        },
        type:'',
        typeTitle:'',
        orderNo:'',
        id:'',
        lineName:'',
      }
    },
    created: function () {

      const {type,typeTitle,lineName,orderNo,id} = this.$route.query

      this.type = type
      this.typeTitle = typeTitle
      this.lineName = lineName
      this.orderNo = orderNo
      this.id = id
      this.fetchData()

      this.contentTitle = `${orderNo}订单 ${typeTitle}`
    },
    destroyed: function () {

    },

    watch: {

    },

    methods: {


      fetchData: function () {


        let apiMethodName = ''

        if(this.type === 'spi'){
          apiMethodName = 'requestGetPcbaRecordGetNotStartRecords'
        }else if(this.type === 'reflow'){
          apiMethodName = 'requestGetPcbaRecordGetNotReflowRecords'
        }else if(this.type === 'aoi'){
          apiMethodName = 'requestGetPcbaRecordGetNotAOIRecords'
        }

        if(!apiMethodName){
          return
        }


        if(this.type === 'spi'){
          request[apiMethodName]({
            // lineName:this.lineName,
            // orderNo:this.orderNo,
            id:this.id,
            page:this.page.currentPage,
            size:this.page.pageSize,
          },res=>{
            if(res.data.code === 200){
              const data = res.data.data
              this.tableHeader = this.makeHeader(data.headers)
              this.tableData = this.makeList(data.pageInfo.list)
              this.page.total = data.pageInfo.total

              this.page.currentPage = data.pageInfo.pageNum
            }
          },res=>{
            this.isRequesting = false
          })
        }else{
          request[apiMethodName]({
            // lineName:this.lineName,
            // orderNo:this.orderNo,
            id:this.id,
            page:this.page.currentPage,
            size:this.page.pageSize,
          },res=>{

            if(res.data.code === 200){

              const data = res.data.data
              this.tableHeader = this.makeAOIHeader(data.headers)
              this.tableData = this.makeAOIList(data.pageInfo.list)
              this.page.total = data.pageInfo.total

              this.page.currentPage = data.pageInfo.pageNum
            }
          },res=>{

          })
        }


      },
      //
      // handleDelete(index, row){
      //   console.log('row',row)
      //   this.fetchDeletePcbaRecord(row.id)
      // },

      handleSizeChange: function (val) {
        this.$emit("handleSizeChange", val)
      },

      handleCurrentChange: function (val) {
        this.page.currentPage = val
        this.fetchData()
      },





      makeAOIHeader(headers){
        // let actionItem = {type: "action", label: this.$getTextByLangKey('操作'), isShow: true,width:'200px'}

        const newHeaders = headers?headers.map(x=>{

          if (x.prop === 'pcbaCode') {
            x.width = 200
            x.type = 'link'
          }
          if (x.prop === 'spiResult') {
            x.width = 120
            x.type = 'link'
          }
          if (x.prop === 'reflowTime') {
            x.type = 'link'
          }
          if (x.prop === 'smtResult') {
            x.width = 120
          }
          if (x.prop === 'aoiResult') {
            x.width = 120
          }
          if (x.prop === 'aoiRepairResult') {
            x.width = 120
          }

          if (x.prop === 'lineName') {
            x.width = 120
          }
          if (x.prop === 'createTime') {
            x.width = 150
          }
          if (x.prop === 'reflowTime') {
            x.width = 150
          }
          if (x.prop === 'smtTime') {
            x.width = 150
          }
          if (x.prop === 'aoiTime') {
            x.width = 150

          }if (x.prop === 'aoiRepairTime') {
            x.width = 150
          }
          x.isShow = true
          return x
        }):[]

        // let actionItem = {type: "action", label: this.$getTextByLangKey('操作'), isShow: true,width:'100px'}
        // newHeaders.push(actionItem)

        return newHeaders
      },

      makeAOIList(list){
        const newList = list?list.map(x=>{
          x.createTime = x.createTime?new Date(x.createTime).format("yyyy/MM/dd hh:mm"):''
          x.reflowTime = x.reflowTime?new Date(x.reflowTime).format("yyyy/MM/dd hh:mm"):''
          x.smtTime = x.smtTime?new Date(x.smtTime).format("yyyy/MM/dd hh:mm"):''
          x.aoiTime = x.aoiTime?new Date(x.aoiTime).format("yyyy/MM/dd hh:mm"):''
          x.aoiRepairTime = x.aoiRepairTime?new Date(x.aoiRepairTime).format("yyyy/MM/dd hh:mm"):''
          x.pcbaCode_link = `./#/wrapper/scanHistoryList?srIds=${x.srIds}`;
          x.reflowTime_link = `./#/wrapper/reflowTempList?reflowTempId=${x.reflowTempId}`;
          x.spiResult_link = `./#/wrapper/spiDetailList?pcbaRecordId=${x.id}`;
          //reflowTempList id
          //spiDetailList pcbaRecordId
          x.action = 'delete'
          x.isShowHighLight = x.showHighLight
          return x
        }):[]
        return newList
      },

      makeHeader(headers){
        // let actionItem = {type: "action", label: this.$getTextByLangKey('操作'), isShow: true,width:'200px'}

        const newHeaders = headers?headers.map(x=>{
          //
          // if (x.prop === 'spiPercent') {
          //   x.type = 'link'
          // }
          // if (x.prop === 'reflowPercent') {
          //   x.type = 'link'
          // }
          // if (x.prop === 'aoiPercent') {
          //   x.type = 'link'
          // }
          x.isShow = true
          return x
        }):[]


        return newHeaders
      },

      makeList(list){
        const newList = list?list.map(x=>{
          x.createTime = x.createTime?new Date(x.createTime).format("yyyy/MM/dd hh:mm"):''

          return x
        }):[]
        return newList
      },



    },


  }
</script>


<style scoped>
  .el-card {
    box-shadow: 0 0;
    border-radius: 0;
    border: none;
  }
</style>


